package javolution.io;

import java.io.CharConversionException;
import java.io.IOException;
import java.io.OutputStream;
import java.io.Writer;
import javolution.lang.Reusable;
import okio.Utf8;
import org.apache.commons.net.telnet.TelnetCommand;

/* loaded from: classes2.dex */
public final class UTF8StreamWriter extends Writer implements Reusable {
    private final byte[] _bytes;
    private char _highSurrogate;
    private int _index;
    private OutputStream _outputStream;

    public UTF8StreamWriter() {
        this._bytes = new byte[2048];
    }

    public UTF8StreamWriter(int i) {
        this._bytes = new byte[i];
    }

    private void flushBuffer() throws IOException {
        OutputStream outputStream = this._outputStream;
        if (outputStream == null) {
            throw new IOException("Stream closed");
        }
        outputStream.write(this._bytes, 0, this._index);
        this._index = 0;
    }

    private void write2(int i) throws IOException {
        if ((i & (-2048)) == 0) {
            byte[] bArr = this._bytes;
            int i2 = this._index;
            bArr[i2] = (byte) ((i >> 6) | 192);
            int i3 = i2 + 1;
            this._index = i3;
            if (i3 >= bArr.length) {
                flushBuffer();
            }
            byte[] bArr2 = this._bytes;
            int i4 = this._index;
            bArr2[i4] = (byte) ((i & 63) | 128);
            int i5 = i4 + 1;
            this._index = i5;
            if (i5 < bArr2.length) {
                return;
            }
        } else if (((-65536) & i) == 0) {
            byte[] bArr3 = this._bytes;
            int i6 = this._index;
            bArr3[i6] = (byte) ((i >> 12) | 224);
            int i7 = i6 + 1;
            this._index = i7;
            if (i7 >= bArr3.length) {
                flushBuffer();
            }
            byte[] bArr4 = this._bytes;
            int i8 = this._index;
            bArr4[i8] = (byte) (((i >> 6) & 63) | 128);
            int i9 = i8 + 1;
            this._index = i9;
            if (i9 >= bArr4.length) {
                flushBuffer();
            }
            byte[] bArr5 = this._bytes;
            int i10 = this._index;
            bArr5[i10] = (byte) ((i & 63) | 128);
            int i11 = i10 + 1;
            this._index = i11;
            if (i11 < bArr5.length) {
                return;
            }
        } else if (((-14680064) & i) == 0) {
            byte[] bArr6 = this._bytes;
            int i12 = this._index;
            bArr6[i12] = (byte) ((i >> 18) | 240);
            int i13 = i12 + 1;
            this._index = i13;
            if (i13 >= bArr6.length) {
                flushBuffer();
            }
            byte[] bArr7 = this._bytes;
            int i14 = this._index;
            bArr7[i14] = (byte) (((i >> 12) & 63) | 128);
            int i15 = i14 + 1;
            this._index = i15;
            if (i15 >= bArr7.length) {
                flushBuffer();
            }
            byte[] bArr8 = this._bytes;
            int i16 = this._index;
            bArr8[i16] = (byte) (((i >> 6) & 63) | 128);
            int i17 = i16 + 1;
            this._index = i17;
            if (i17 >= bArr8.length) {
                flushBuffer();
            }
            byte[] bArr9 = this._bytes;
            int i18 = this._index;
            bArr9[i18] = (byte) ((i & 63) | 128);
            int i19 = i18 + 1;
            this._index = i19;
            if (i19 < bArr9.length) {
                return;
            }
        } else if (((-201326592) & i) == 0) {
            byte[] bArr10 = this._bytes;
            int i20 = this._index;
            bArr10[i20] = (byte) ((i >> 24) | TelnetCommand.EL);
            int i21 = i20 + 1;
            this._index = i21;
            if (i21 >= bArr10.length) {
                flushBuffer();
            }
            byte[] bArr11 = this._bytes;
            int i22 = this._index;
            bArr11[i22] = (byte) (((i >> 18) & 63) | 128);
            int i23 = i22 + 1;
            this._index = i23;
            if (i23 >= bArr11.length) {
                flushBuffer();
            }
            byte[] bArr12 = this._bytes;
            int i24 = this._index;
            bArr12[i24] = (byte) (((i >> 12) & 63) | 128);
            int i25 = i24 + 1;
            this._index = i25;
            if (i25 >= bArr12.length) {
                flushBuffer();
            }
            byte[] bArr13 = this._bytes;
            int i26 = this._index;
            bArr13[i26] = (byte) (((i >> 6) & 63) | 128);
            int i27 = i26 + 1;
            this._index = i27;
            if (i27 >= bArr13.length) {
                flushBuffer();
            }
            byte[] bArr14 = this._bytes;
            int i28 = this._index;
            bArr14[i28] = (byte) ((i & 63) | 128);
            int i29 = i28 + 1;
            this._index = i29;
            if (i29 < bArr14.length) {
                return;
            }
        } else if ((Integer.MIN_VALUE & i) != 0) {
            throw new CharConversionException("Illegal character U+" + Integer.toHexString(i));
        } else {
            byte[] bArr15 = this._bytes;
            int i30 = this._index;
            bArr15[i30] = (byte) ((i >> 30) | 252);
            int i31 = i30 + 1;
            this._index = i31;
            if (i31 >= bArr15.length) {
                flushBuffer();
            }
            byte[] bArr16 = this._bytes;
            int i32 = this._index;
            bArr16[i32] = (byte) (((i >> 24) & 63) | 128);
            int i33 = i32 + 1;
            this._index = i33;
            if (i33 >= bArr16.length) {
                flushBuffer();
            }
            byte[] bArr17 = this._bytes;
            int i34 = this._index;
            bArr17[i34] = (byte) (((i >> 18) & 63) | 128);
            int i35 = i34 + 1;
            this._index = i35;
            if (i35 >= bArr17.length) {
                flushBuffer();
            }
            byte[] bArr18 = this._bytes;
            int i36 = this._index;
            bArr18[i36] = (byte) (((i >> 12) & 63) | 128);
            int i37 = i36 + 1;
            this._index = i37;
            if (i37 >= bArr18.length) {
                flushBuffer();
            }
            byte[] bArr19 = this._bytes;
            int i38 = this._index;
            bArr19[i38] = (byte) (((i >> 6) & 63) | 128);
            int i39 = i38 + 1;
            this._index = i39;
            if (i39 >= bArr19.length) {
                flushBuffer();
            }
            byte[] bArr20 = this._bytes;
            int i40 = this._index;
            bArr20[i40] = (byte) ((i & 63) | 128);
            int i41 = i40 + 1;
            this._index = i41;
            if (i41 < bArr20.length) {
                return;
            }
        }
        flushBuffer();
    }

    @Override // java.io.Writer, java.io.Closeable, java.lang.AutoCloseable
    public void close() throws IOException {
        if (this._outputStream != null) {
            flushBuffer();
            this._outputStream.close();
            reset();
        }
    }

    @Override // java.io.Writer, java.io.Flushable
    public void flush() throws IOException {
        flushBuffer();
        this._outputStream.flush();
    }

    @Override // javolution.lang.Reusable
    public void reset() {
        this._highSurrogate = (char) 0;
        this._index = 0;
        this._outputStream = null;
    }

    public UTF8StreamWriter setOutput(OutputStream outputStream) {
        if (this._outputStream == null) {
            this._outputStream = outputStream;
            return this;
        }
        throw new IllegalStateException("Writer not closed or reset");
    }

    public UTF8StreamWriter setOutputStream(OutputStream outputStream) {
        return setOutput(outputStream);
    }

    public void write(char c) throws IOException {
        if (c < 55296 || c > 57343) {
            write((int) c);
        } else if (c < 56320) {
            this._highSurrogate = c;
        } else {
            write(((this._highSurrogate - 55296) << 10) + (c - Utf8.LOG_SURROGATE_HEADER) + 65536);
        }
    }

    @Override // java.io.Writer
    public void write(int i) throws IOException {
        if ((i & (-128)) != 0) {
            write2(i);
            return;
        }
        byte[] bArr = this._bytes;
        int i2 = this._index;
        bArr[i2] = (byte) i;
        int i3 = i2 + 1;
        this._index = i3;
        if (i3 >= bArr.length) {
            flushBuffer();
        }
    }

    public void write(CharSequence charSequence) throws IOException {
        int length = charSequence.length();
        int i = 0;
        while (i < length) {
            int i2 = i + 1;
            char charAt = charSequence.charAt(i);
            if (charAt < 128) {
                byte[] bArr = this._bytes;
                int i3 = this._index;
                bArr[i3] = (byte) charAt;
                int i4 = i3 + 1;
                this._index = i4;
                if (i4 >= bArr.length) {
                    flushBuffer();
                }
            } else {
                write(charAt);
            }
            i = i2;
        }
    }

    @Override // java.io.Writer
    public void write(String str, int i, int i2) throws IOException {
        int i3 = i2 + i;
        while (i < i3) {
            int i4 = i + 1;
            char charAt = str.charAt(i);
            if (charAt < 128) {
                byte[] bArr = this._bytes;
                int i5 = this._index;
                bArr[i5] = (byte) charAt;
                int i6 = i5 + 1;
                this._index = i6;
                if (i6 >= bArr.length) {
                    flushBuffer();
                }
            } else {
                write(charAt);
            }
            i = i4;
        }
    }

    @Override // java.io.Writer
    public void write(char[] cArr, int i, int i2) throws IOException {
        int i3 = i2 + i;
        while (i < i3) {
            int i4 = i + 1;
            char c = cArr[i];
            if (c < 128) {
                byte[] bArr = this._bytes;
                int i5 = this._index;
                bArr[i5] = (byte) c;
                int i6 = i5 + 1;
                this._index = i6;
                if (i6 >= bArr.length) {
                    flushBuffer();
                }
            } else {
                write(c);
            }
            i = i4;
        }
    }
}
